<?php
/**
 * Created by PhpStorm.
 * User: wt1
 * Date: 2016/11/7
 * Time: 11:16
 */
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redis;

class Order extends Model
{
	public function create_address($post){
		DB::table("order_address")->insert($post);
		return true;
	}
	public function getAddrByCustID($cust_id,$select = array() ){
		if($select){
			return DB::table("order_address")->select($select)->where('cust_id',$cust_id)->get();
		}else{
			return DB::table("order_address")->where('cust_id',$cust_id)->get();
		}
	}
	public function after_verify_status($basic_order,$order){
		switch ($basic_order->cust_type) {
			case '0'://经销商
				$this->after_verify_status_dealer($basic_order,$order);
				break;
			case '1'://终端
				$this->after_verify_status_factory($basic_order,$order);
				break;
			case '2'://科研
				$this->after_verify_status_science($basic_order,$order);
				break;
		}
	}
	public function after_verify_status_science($basic_order,$order){
		$property = $order->property;
		$status = $order->status;
		$cust_id = $basic_order->cust_id;
		$order_list_id = $order->id;
		switch ($property) {
			case 'poison':
				if($status == 20 || $status == 10){
					$check = DB::table("cust_material")
							->where('cust_id',$cust_id)
							->where('bussines_legal_card_status',1)
							->where('legal_card_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(11,$order_list_id);
					}
				}
				if($status == 21){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('shop_card_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(12,$order_list_id);
					}
				}
				if($status == 22){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('transport_proxy_status',1)
							->where('transport_contract_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(23,$order_list_id);
					}
				}
				if($status == 23){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('transport_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(40,$order_list_id);
					}
				}
				break;
			case 'detonate':
				if($status == 20){
					$check = DB::table("cust_material")
							->where('cust_id',$cust_id)
							->where('bussines_legal_card_status',1)
							->count();
					if($check > 0){
						$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('buy_card_status',1)
							->where('illustrate_status',1)
							->count();
						if($check > 0){
							$this->update_order_list_status_by_id(40,$order_list_id);
						}
					}
				}
				break;
		}
	}
	public function after_verify_status_factory($basic_order,$order){
		$property = $order->property;
		$status = $order->status;
		$cust_id = $basic_order->cust_id;
		$order_list_id = $order->id;
		switch ($property) {
			case 'poison':
				if($status == 20 || $status == 10){
					$check = DB::table("cust_material")
							->where('cust_id',$cust_id)
							->where('license_status',1)
							->where('legal_card_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(11,$order_list_id);
					}
				}
				if($status == 21){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('shop_card_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(12,$order_list_id);
					}
				}
				if($status == 22){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('transport_proxy_status',1)
							->where('transport_contract_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(23,$order_list_id);
					}
				}
				if($status == 23){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('transport_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(40,$order_list_id);
					}
				}
				break;
			case 'detonate':
				if($status == 20){
					$check = DB::table("cust_material")
							->where('cust_id',$cust_id)
							->where('license_status',1)
							->where('legal_card_status',1)
							->count();
					if($check > 0){
						$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('buy_card_status',1)
							->where('illustrate_status',1)
							->count();
						if($check > 0){
							$this->update_order_list_status_by_id(40,$order_list_id);
						}
					}
				}
				break;
		}
	}
	public function after_verify_status_dealer($basic_order,$order){
		$property = $order->property;
		$status = $order->status;
		$cust_id = $basic_order->cust_id;
		$order_list_id = $order->id;
		switch ($property) {
			case 'poison':
				if($status == 20){
					$check = DB::table("cust_material")
							->where('cust_id',$cust_id)
							->where('license_status',1)
							->where('legal_card_status',1)
							->where('market_card_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(11,$order_list_id);
					}
				}
				if($status == 21){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('shop_card_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(12,$order_list_id);
					}
				}
				if($status == 22){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('transport_proxy_status',1)
							->where('transport_contract_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(23,$order_list_id);
					}
				}
				if($status == 23){
					$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('transport_status',1)
							->count();
					if($check > 0){
						$this->update_order_list_status_by_id(40,$order_list_id);
					}
				}
				break;
			case 'detonate':
				if($status == 20){
					$check = DB::table("cust_material")
							->where('cust_id',$cust_id)
							->where('license_status',1)
							->where('legal_card_status',1)
							->where('dcs_card_status',1)
							->count();
					if($check > 0){
						$check = DB::table("order_list")
							->where('id',$order_list_id)
							->where('buy_card_status',1)
							->count();
						if($check > 0){
							$this->update_order_list_status_by_id(40,$order_list_id);
						}
					}
				}
				break;
		}
	}
	public function get_product_goods_by_id($ids,$field = ''){
		$rs = DB::table("tp_goods");
		if($field){
			$rs->select($field);
		}
		$rs->whereIn('goods_id',$ids);
		return $rs->get();
	}
	public function get_product_pirce_list($query){
		// DB::connection()->enableQueryLog();
		if(!isset($query['page'])){
			$query['page'] = 1;
		}
		if(!isset($query['size'])){
			$query['size'] = 10;
		}
		$rs = DB::table("tp_spec_goods_price");
		if(isset($query['is_default'])){
			$rs->where('is_default',$query['is_default']);
		}
		if(isset($query['status'])){
			$rs->where('status',$query['status']);
		}
		if(isset($query['store_count'])){
			$rs->where('store_count','>','0');
		}
		if(isset($query['danger_type'])){
			if($query['danger_type'] == true){
				$rs->whereNotNull('danger_type');
			}else{
				$rs->whereNull('danger_type');  
			}
		}
		if(isset($query['name'])){
			$rs->where('name','like','%'.$query['name'].'%');
		}
		if(isset($query['groupBy'])){
			$rs->groupBy($query['groupBy']);
		}
		if(isset($query['count'])){
			return $rs->count();
		}
		$rs->offset(($query['page']-1)*$query['size'])->limit($query['size']);
		// $rs->get();
		// print_r(DB::getQueryLog());die();
		return $rs->get();
	}
	public function update_goods_price_danger_type($goods_id,$data){
		$update = array();
		if(isset($data['danger_type'])){
			$update['danger_type'] = $data['danger_type'];
		}
		if($update){
			if(is_array($goods_id)){
				DB::table("tp_spec_goods_price")->whereIn('goods_id',$goods_id)->update($update);	
			}else{
				DB::table("tp_spec_goods_price")->where('goods_id',$goods_id)->update($update);	
			}
		}
	}
	public function get_shop_card_by_id($shop_card_id){
		return DB::table("shop_card")->where('id',$shop_card_id)->first();
	}
	public function get_cust_material($cust_id,$field = ''){
		$rs = DB::table("cust_material");
		if($field){
			$rs->select($field);
		}
		return $rs->where('cust_id',$cust_id)->first();
	}
	public function verify_cust_material($cust_id,$field,$post,$order_info = array()){
		$update = array($field.'_status'=>1);
		switch ($field) {
			case 'license':
				$update['credit_code'] = $post['credit_code'];
				$update['legal_name'] = $post['legal_name'];
				$post['license_validity'] .= ' 00:00:01';
				$post['license_validity'] = strtotime($post['license_validity']);
				$update['license_validity'] = $post['license_validity'];
				DB::table("cust_material")->where('cust_id',$cust_id)->WhereNotNull($field)->update($update);
				break;
			case 'legal_card':
				$update['legal_name'] = $post['legal_name'];
				$post['legal_card_validity'] .= ' 00:00:01';
				$post['legal_card_validity'] = strtotime($post['legal_card_validity']);
				$update['legal_card_validity'] = $post['legal_card_validity'];
				DB::table("cust_material")->where('cust_id',$cust_id)->WhereNotNull($field)->update($update);
				break;
			case 'market_card':
				$update['market_cate'] = $post['market_cate'];
				$post['market_card_validity'] .= ' 00:00:01';
				$post['market_card_validity'] = strtotime($post['market_card_validity']);
				$update['market_card_validity'] = $post['market_card_validity'];
				DB::table("cust_material")->where('cust_id',$cust_id)->WhereNotNull($field)->update($update);
				break;
			case 'shop_card':
				$shop_card_id = $this->get_order_material_by_order_id($post['id'])->shop_card_id;
				if(DB::table("shop_card")->where('card_code',$post['card_code'])->count() == 0){
					$update['card_code'] = $post['card_code'];
					$update['card_type'] = $post['card_type'];
					$update['used'] = $post['used'];
					$update['product_id'] = $post['product_id'];
					$update['product_size'] = $post['product_size'];
					$update['product_standard'] = $post['product_standard'];
					$post['etime'] .= ' 00:00:01';
					$post['etime'] = strtotime($post['etime']);
					$update['etime'] = $post['etime'];
					unset($update['shop_card_status']);
					DB::table("shop_card")->where('cust_id',$cust_id)->where('id',$shop_card_id)->WhereNotNull('pic')->update($update);
					DB::table("order_list")->where('order_id',$post['id'])->update(array('shop_card_status'=>1));
				}
				
				break;
			case 'transport_proxy':
				DB::table("order_list")->where('order_id',$post['id'])->update($update);
				break;
			case 'transport_contract':
				DB::table("order_list")->where('order_id',$post['id'])->update($update);
				break;
			case 'transport':
				if($post['transport']){
					$pic = $post['transport'];
					$name = $pic->getClientOriginalName();//得到图片名；
					$path = date('Y-m');
					$fileName = rand(1,999).'_'.$name;//生成新的的文件名
					Storage::disk('material')->makeDirectory($path);
					$filePath = $path.'/'.$fileName;
					Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
					$insert = array();
					$insert['cust_id'] = $cust_id;
					$insert['transport'] = $filePath;
					$insert['transport_code'] = $post['transport_code'];
					$insert['shop_card_code'] = $post['shop_card_code'];
					$insert['product_id'] = $post['product_id'];
					$insert['product_size'] = $post['product_size'];
					$insert['product_standard'] = $post['product_standard'];

					$post['stime'] .= ' 00:00:01';
					$post['stime'] = strtotime($post['stime']);
					$insert['stime'] = $post['stime'];

					$post['etime'] .= ' 00:00:01';
					$post['etime'] = strtotime($post['etime']);
					$insert['etime'] = $post['etime'];
					$transport_card_id = DB::table("transport_card")->insertGetId($insert);
					DB::table("order_material")->where('order_list_id',$order_info->id)->update(array('transport_id'=>$transport_card_id));
					DB::table("order_list")->where('order_id',$post['id'])->update(array('transport_status'=>1));
				}
				break;
			case 'buy_card':
				DB::table("order_list")->where('order_id',$post['id'])->update($update);
				break;
			case 'illustrate':
				DB::table("order_list")->where('order_id',$post['id'])->update($update);
				break;
			case 'dcs_card':
				$post['dcs_card_validity'] .= ' 00:00:01';
				$post['dcs_card_validity'] = strtotime($post['dcs_card_validity']);
				$update['dcs_card_validity'] = $post['dcs_card_validity'];
				$update['dcs_card_product'] = $post['dcs_card_product'];
				DB::table("cust_material")->where('cust_id',$cust_id)->WhereNotNull($field)->update($update);
				break;
			case 'bussines_legal_card':
				$update['legal_name'] = $post['legal_name'];
				$post['bussines_legal_card_validity'] .= ' 00:00:01';
				$post['bussines_legal_card_validity'] = strtotime($post['bussines_legal_card_validity']);
				$update['bussines_legal_card_validity'] = $post['bussines_legal_card_validity'];
				DB::table("cust_material")->where('cust_id',$cust_id)->WhereNotNull($field)->update($update);
				break;
		}
	}

	public function verify_cust_materialFail($cust_id,$field,$remark){
		DB::table("cust_material")->where('cust_id',$cust_id)->update(array($field.'_status'=>2,$field.'_remark'=>$remark));
	}

	public function getAddrById($address_id){
		return DB::table("order_address")->where('id',$address_id)->first();
	}

	public function get_order_verify_list($get){
		// DB::connection()->enableQueryLog();
		$rs = DB::table("order_list")
				->leftJoin('order_basic','order_list.order_basic_id','=','order_basic.id')
				->leftJoin('cust_material','order_basic.cust_id','=','cust_material.cust_id')
				->leftJoin('order_material','order_list.id','=','order_material.order_list_id')
				// ->leftJoin('order_address','order_basic.address_id','=','order_address.id')
				;
		if(isset($get['s'])){
			// $rs->where('order_list.verify','=',0);
			if($get['s'] == 'all'){

			}elseif($get['s'] == 'transport'){
				$rs->where('order_list.status','=',23);
			}else{
				$rs->where('order_list.status','<',$get['s']);
				$rs->where('order_list.status','>=',$get['s']-10);
				if($get['p'] == 'poison'){
					$rs->where('order_list.status','!=',23);
				}
			}
		}
		if(isset($get['order_id'])){
			$rs->where('order_list.order_id','=',$get['order_id']);
		}
		if(isset($get['p'])){
			$rs->where('order_list.property','=',$get['p']);
		}
		if(isset($get['cust_name'])){
			$rs->where('order_basic.cust_name','=',$get['cust_name']);
		}
		// if(isset($get['name'])){
		//	 $rs->where('order_address.name','=',$get['name']);
		// }
		if(isset($get['total_price'])){
			$rs->where('order_list.total_price','=',$get['total_price']);
		}
		if(isset($get['already_pay'])){
			$rs->where('order_list.already_pay','=',$get['already_pay']);
		}
		if(isset($get['stime'])){
			$get['stime'] .= ':00';
			$get['stime'] = strtotime($get['stime']);
			$rs->where('order_list.create_time','>=',$get['stime']);
		}
		if(isset($get['etime'])){
			$get['etime'] .= ':00';
			$get['etime'] = strtotime($get['etime']);
			$rs->where('order_list.create_time','<=',$get['etime']);
		}
		if(isset($get['limit'])){
			$limit = $get['limit'];
		}else{
			$limit = 1;
		}
		return $rs->paginate($limit);
		// print_r(DB::getQueryLog());die();
	}
	public function statistics_order_manage(){
		// $cache = Redis::get('statistics_order_manage');
		// if($cache){
		// 	return json_decode($cache,true);
		// }else{
			$result = array();
			$result['count_10'] = $this->statistics_order_manage_by_status(10);
			$result['count_20'] = $this->statistics_order_manage_by_status(20);
			$result['count_30'] = $this->statistics_order_manage_by_status(30);
			$result['count_40'] = $this->statistics_order_manage_by_status(40);
			$result['count_50'] = $this->statistics_order_manage_by_status(50);
			$result['count_60'] = $this->statistics_order_manage_by_status(60);
			$result['count_70'] = $this->statistics_order_manage_by_status(70);
			$result['count_80'] = $this->statistics_order_manage_by_status(80);
			$result['count_90'] = $this->statistics_order_manage_by_status(90);
			$result['count_all'] = $this->statistics_order_manage_by_status('all');
			// Redis::setex('statistics_order_manage', 60*10, json_encode($result));
			return $result;
		// }
	}
	public function statistics_order_manage_by_status($status){
		$rs = DB::table("order_list");
		if($status != 'all'){
			$rs->where('order_list.status','<',$status);
			$rs->where('order_list.status','>=',$status-10);
		}
		return $rs->count();
	}
	public function get_order_list($get){
		// DB::connection()->enableQueryLog();
		$rs = DB::table("order_list")
				->leftJoin('order_basic','order_list.order_basic_id','=','order_basic.id')
				->leftJoin('order_address','order_basic.address_id','=','order_address.id')
				;
		if(isset($get['s'])){
			// $rs->where('order_list.verify','=',0);
			if($get['s'] == 'all'){

			}else{
				$rs->where('order_list.status','<',$get['s']);
				$rs->where('order_list.status','>=',$get['s']-10);
			}
		}
		if(isset($get['order_id'])){
			$rs->where('order_list.order_id','=',$get['order_id']);
		}
		if(isset($get['property'])){
			$rs->where('order_list.property','=',$get['property']);
		}
		if(isset($get['cust_name'])){
			$rs->where('order_basic.cust_name','=',$get['cust_name']);
		}
		if(isset($get['name'])){
			$rs->where('order_address.name','=',$get['name']);
		}
		if(isset($get['total_price'])){
			$rs->where('order_list.total_price','=',$get['total_price']);
		}
		if(isset($get['already_pay'])){
			$rs->where('order_list.already_pay','=',$get['already_pay']);
		}
		if(isset($get['stime'])){
			$get['stime'] .= ':00';
			$get['stime'] = strtotime($get['stime']);
			$rs->where('order_list.create_time','>=',$get['stime']);
		}
		if(isset($get['etime'])){
			$get['etime'] .= ':00';
			$get['etime'] = strtotime($get['etime']);
			$rs->where('order_list.create_time','<=',$get['etime']);
		}
		if(isset($get['limit'])){
			$limit = $get['limit'];
		}else{
			$limit = 1;
		}
		return $rs->paginate($limit);
		// print_r(DB::getQueryLog());die();
	}

	public function get_order_basic_by_id($basic_id){
		return DB::table("order_basic")->where('id',$basic_id)->first();
	}
	public function get_list_by_basic_id($basic_id){
		return DB::table("order_list")->where('order_basic_id',$basic_id)->get()->toArray();
	}
	public function get_order_info_by_order_id($order_id){
		return DB::table("order_list")->where('order_id',$order_id)->first();
	}
	public function get_order_info_by_order_list_id($order_list_id){
		return DB::table("order_list")->where('id',$order_list_id)->first();
	}
	public function after_upload_contract($order){
		$basic_order = $this->get_order_basic_by_id($order->order_basic_id);
		$this->after_verify_status($basic_order,$order);
	}
    public function update_order_ignore_contract($order_id){
        $update = array('status'=>10,'ignore_contract'=>1);
        return DB::table("order_list")->where('order_id',$order_id)->update($update);
    }
	public function update_order_list_status($status,$order_id){
		$update = array('status'=>$status);
		return DB::table("order_list")->where('order_id',$order_id)->update($update);
	}
	public function update_order_list_status_by_id($status,$order_list_id){
		$update = array('status'=>$status,'verify'=>1);
		return DB::table("order_list")->where('id',$order_list_id)->update($update);
	}
	public function upload_order_material($data,$order_id){
		if(!$order_id){
			return false;
		}

		$count = DB::table("order_material")->where('order_list_id',$order_id)->count();
		if($count == 0){
			unset($data['order_id']);
			$data['order_list_id'] = $order_id;
			return DB::table("order_material")->insert($data);
		}
	}
	public function get_order_material_by_order_id($order_id){
		$order_id = $this->get_order_info_by_order_id($order_id)->id;
		return DB::table("order_material")->where('order_list_id',$order_id)->first();
	}

	public function get_order_material_by_order_list_id($order_id){
		return DB::table("order_material")->where('order_list_id',$order_id)->first();
	}

	public function update_cust_material($data){
		if($data['order_id']){
			$order = $this->get_order_info_by_order_id($data['order_id']);
			$order_basic = $this->get_order_basic_by_id($order->order_basic_id);
			$new_status = $order->status + 10;
			$order_id = $data['order_id'];
			if(isset($data['shop_card'])){
				$shop_card_id = DB::table("order_material")->select('shop_card_id')->where('order_list_id',$order->id)->first();
				if(!$shop_card_id->shop_card_id){
					unset($data['order_id']);
					// $data['order_list_id'] = $order->id;
					$data['pic'] = $data['shop_card'];
					$data['cust_id'] = $order_basic->cust_id;
					unset($data['shop_card']);
					$shop_card_id = DB::table("shop_card")->insertGetId($data);
				}else{
					$shop_card_id = $shop_card_id->shop_card_id;
				}
				DB::table("order_list")->where('order_id',$order_id)->update(array('status'=>$new_status));
				DB::table("order_material")->where('order_list_id',$order->id)->update(array('shop_card_id'=>$shop_card_id));
			}elseif(isset($data['transport_proxy'])){
				$update = array();
				DB::table("order_list")->where('order_id',$order_id)->update(array('status'=>$new_status));
				if(isset($data['contract'])){
					$update['contract'] = $data['contract'];
				}
				if($update){
					$update['transport_proxy'] = $data['transport_proxy'];
					DB::table("order_material")->where('order_list_id',$order->id)->update($update);	
				}
			}elseif(isset($data['dcs_card'])){
				unset($data['order_id']);
				$order_basic = $this->get_order_basic_by_id($order->order_basic_id);
				$data['cust_id'] = $order_basic->cust_id;
				$buy_card = $data['buy_card'];
				unset($data['buy_card']);
				$cnt = DB::table("cust_material")->where('cust_id',$data['cust_id'])->count();
				if($cnt == 0){
					$data['cust_name'] = $order_basic->cust_name;
					$data['cust_type'] = $order_basic->cust_type;
					$data['upload_time'] = time();
					DB::table("cust_material")->insert($data);
				}else{
					$cust_id = $data['cust_id'];
					unset($data['cust_id']);
					$data['upload_time'] = time();
					DB::table("cust_material")->where('cust_id',$cust_id)->update($data);
				}
				DB::table("order_material")->where('order_list_id',$order->id)->update(array('buy_card'=>$buy_card));  
				DB::table("order_list")->where('order_id',$order_id)->update(array('status'=>$new_status,'verify'=>1));
			}else{
				unset($data['order_id']);
				$order_basic = $this->get_order_basic_by_id($order->order_basic_id);
				$data['cust_id'] = $order_basic->cust_id;
				if(isset($data['illustrate'])){
					$illustrate = $data['illustrate'];
					unset($data['illustrate']);
				}
				if(isset($data['buy_card'])){
					$buy_card = $data['buy_card'];
					unset($data['buy_card']);
				}
				$cnt = DB::table("cust_material")->where('cust_id',$data['cust_id'])->count();
				if($cnt == 0){
					$data['cust_name'] = $order_basic->cust_name;
					$data['cust_type'] = $order_basic->cust_type;
					$data['upload_time'] = time();
					DB::table("cust_material")->insert($data);
				}else{
					$cust_id = $data['cust_id'];
					unset($data['cust_id']);
					$data['upload_time'] = time();
					DB::table("cust_material")->where('cust_id',$cust_id)->update($data);
				}

				DB::table("order_list")->where('order_id',$order_id)->update(array('status'=>$new_status,'verify'=>1));
				$update_order_material = array();
				if(isset($illustrate)){
					$update_order_material['illustrate'] = $illustrate;
				}
				if(isset($buy_card)){
					$update_order_material['buy_card'] = $buy_card;
				}
				if($update_order_material){
					DB::table("order_material")->where('order_list_id',$order->id)->update($update_order_material);  
				}
			}
			
		}
	}
	public function get_order_product_by_order_ids($order_ids){
		$result = array();
		$rs = DB::table("order_product")->whereIn('order_list_id',$order_ids)->get()->toArray();
		if($rs){
			$product_ids = array();
			foreach ($rs as $key => $v) {
				$product_ids[] = $v->product_id;
				$result[$v->order_list_id][$v->product_id] = array('num'=>$v->num,'price'=>$v->price,'total'=>number_format($v->num*$v->price,2));
			}
			if($product_ids){
				$product_list = $this->get_product_by_ids($product_ids);
				foreach ($result as $key => $value) {
					foreach ($value as $k => $v) {
						$result[$key][$k]['detail'] = $product_list[$k];
					}
				}
			}
		}
		return $result;
	}
	public function get_product_by_ids($product_ids){
		$result = array();
		$rs = DB::table("product")->whereIn('id',$product_ids)->get()->toArray();
		foreach ($rs as $key => $value) {
			$result[$value->id] = $value;
		}
		return $result;
	}
	public function create_order_basic($post){
		$insert = array();
		$insert['rule_type'] = $post['rule_type'];
		$insert['cust_name'] = $post['cust_name'];
		$insert['cust_id'] = $post['cust_id'];
		$insert['order_period'] = $post['order_period'];
		$insert['sales_org'] = $post['sales_org'];
		$insert['create_company'] = $post['create_company'];
		$insert['clerk'] = $post['clerk'];
		$insert['clerk_department'] = $post['clerk_department'];
		$insert['clerk_area'] = $post['clerk_area'];
		$insert['sales_support'] = $post['sales_support'];
		$insert['cust_type'] = $post['cust_type'];
		$insert['payment'] = $post['payment'];
		$insert['currency'] = $post['currency'];
		$insert['deliver'] = $post['deliver'];
		$insert['address_id'] = $post['address_id'];
		$insert['supplier'] = $post['supplier'];
		$insert['invoice_type'] = $post['invoice_type'];
		$insert['discount'] = $post['discount'];
		$insert['remark'] = $post['remark'];
		$order_basic_id = DB::table("order_basic")->insertGetId($insert);

		DB::table("order_group")->insert(array('order_basic_id'=>$order_basic_id,'pack'=>json_encode($post['product_obj'])));

		return $order_basic_id;
	}
	public function create_product($post){
		$rs = DB::table("product")->select('id')->where('stock_num',$post['stock_num'])->first();
		if($rs){
			return $rs->id;
		}

		$post['property'] = $this->process_transl_property($post['property']);
		$insert = array();
		$insert['stock_num'] = $post['stock_num'];
		$insert['property'] = $post['property'];
		$insert['name'] = $post['name'];
		$insert['size'] = $post['size'];
		$insert['standard'] = $post['standard'];
		$insert['pack'] = $post['pack'];
		return DB::table("product")->insertGetId($insert);
	}
	public function create_order($order){
		$order_product = $order['order_product'];
		unset($order['order_product']);
		$order['create_time'] = time();
		$order_id = DB::table("order_list")->insertGetId($order);
		$this->create_order_product($order_product,$order_id);
		return $order_id;
	}
	public function create_order_product($order_product,$order_id){
		if($order_product && $order_id){
			foreach ($order_product as $key => $value) {
				$insert = array();
				$insert['order_list_id'] = $order_id;
				$insert['product_id'] = $value['product_id'];
				$insert['num'] = $value['num'];
				$insert['price'] = $value['price'];
				DB::table("order_product")->insert($insert);
			}
		}
	}
	public function process_transl_property($property){
		$config = config('order.order.db_translate_product_attr');
		if(isset($config[$property])){
			$property = $config[$property];
		}else{
			$property = '普通';
		}
		switch ($property) {
			case '易制爆':
				$property = 'detonate';
				break;
			case '易制毒':
				$property = 'poison';
				break;
			default:
				$property = 'normal';
				break;
		}
		return $property;
	}
	public function create_order_num(){
		$order_num = time().rand(1,9).rand(1,9).rand(1,9).rand(1,9);
		return $order_num;
	}
}